package com.xian.demo;

import java.util.Scanner;

/**
 * 需求
 * 模拟购物车模块的功能，需要实现添加商品到购物车中去，同时需要提供修改商品的
 * 购买数量，结算商品价格功能（请使用面向对象编程来解决）。
 * <p>
 * 分析
 * 购物车中的每个商品都是一个对象，需要定义一个商品类。
 * 购物车本身也是一个对象:可以使用数组对象代表它。完成界面架构，让用户选择操作的功能。
 */
public class ShopCarTest {
    public static void main(String[] args) {
        //1.定义一个商品类，用于后期闯将商品名称

        //2.定义购物车对象：使用一个数组对象表示
        Goods[] shopCar = new Goods[100]; //地址[null,,,]

        while (true) {
            //3.搭建操作架构
            System.out.println("请选择如下命令进行操作:");
            System.out.println("添加商品到购物车:add");
            System.out.println("查询购物车商品展示:query");
            System.out.println("修改商品购买数量:update");
            System.out.println("结算购买商品的金额:pay");

            Scanner sc = new Scanner(System.in);
            System.out.println("请输入命令；");
            String command = sc.next();
            switch (command) {
                case "add":
                    addGoods(shopCar, sc);
                    break;
                case "query":
                    queryGoods(shopCar);
                    break;
                case "update":
                    updateGoods(shopCar, sc);
                    break;
                case "pay":
                    pay(shopCar);
                    break;
                default:
                    System.out.println("没有该功能！！！！");
            }
        }

    }

    /*
    *
    * 需求
      当用户输入了pay命令后，需要展示全部购买的商品信息和总金额。
      分析
      定义求和变量，遍历购物车数组中的全部商品，累加其单价*购买数量。
* */
    public static void pay(Goods[] shopCar) {
        queryGoods(shopCar);
        //1.定义一个求和变量累加金额
        double money = 0;
        //2.遍历购物车数组，累加其单价*购买数量
        for (int i = 0; i < shopCar.length; i++) {
            Goods goods = shopCar[i];
            if (goods != null) {
                money += (goods.price * goods.buyNumber);
            } else {
                break;
            }
        }
        System.out.println("订单总金额:" + money);
    }

    public static void updateGoods(Goods[] shopCar, Scanner sc) {
        //用户输入要修改的ID，根据id查询出要修改的对象
        while (true) {
            System.out.println("请输入要修改的id:");
            int id = sc.nextInt();
            Goods goods = getGoodsById(shopCar, id);
            if (goods == null) {
                System.out.println("对不起，没有该商品！！！");

            } else {
                System.out.println("请您输入:" + goods.name + "商品数量:");
                int buyNumber = sc.nextInt();
                goods.buyNumber = buyNumber;
                System.out.println("修改完成！！！");
                queryGoods(shopCar);
                break;
            }
        }

    }

    public static Goods getGoodsById(Goods[] shopCar, int id) {
        for (int i = 0; i < shopCar.length; i++) {
            Goods goods = shopCar[i];
            if (goods != null) {
                if (goods.id == id) {
                    return goods;
                }
            } else {
                return null;
            }
        }
        return null;
    }

    public static void queryGoods(Goods[] shopCar) {
        System.out.println("==================查询购物车如下==================");
        System.out.println("编号\t\t名称\t\t价格\t\t\t购买数量");
        for (int i = 0; i < shopCar.length; i++) {
            Goods goods = shopCar[i];
            if (goods != null) {
                System.out.println(goods.id + "\t\t" + goods.name + " \t\t\t" + goods.price + "\t\t\t" + goods.buyNumber);
            } else {
                break;
            }
        }

    }

    public static void addGoods(Goods[] shopCar, Scanner sc) {

        //1.录入用户输入购买的商品信息
        System.out.println("请输入编号:");
        int id = sc.nextInt();
        System.out.println("请输入名称:");
        String name = sc.next();
        System.out.println("请输入数量:");
        int buyNumber = sc.nextInt();
        System.out.println("请输入价格:");
        double price = sc.nextDouble();

        //2.把这些信息封装成对象
        Goods goods = new Goods();
        goods.id = id;
        goods.name = name;
        goods.buyNumber = buyNumber;
        goods.price = price;

        //3.把这个对象添加到数组
        for (int i = 0; i < shopCar.length; i++) {
            if (shopCar[i] == null) {
                shopCar[i] = goods; //地址
                break;
            }
        }
        System.out.println("您的商品：" + goods.name + "添加成功！！！");
    }
}
